//在线社区
Vue.component('community_forum', {
	//修改密码组件
	template: '<div class="community_list">\
						<li class="type_title">在线社区</li>\
						<!--社区列表-->\
						<div class="community_news">\
							<div class="content cursor" @click="go_detail(item)" v-for="(item,index) in forum">\
								<!--上半部分-->\
								<div >\
									<img :src=" item.HeadImage ? item.HeadImage : \'imgs/icon.jpg\'" alt="" />\
									<div class="my_flex my_flex_col">\
										<span>{{item.Title | cut_text(48)}}</span>\
										<span>{{item.CreateTimeText}}</span>\
									</div>\
								</div>\
								<div>\
									<span>{{item.ContentShort}}</span>\
								</div>\
								<div>\
									<!--循环图片,返回数所中没有图片-->\
								</div>\
								<!--下半部分-->\
								<div class="icons my_flex">\
									<!--<li class="my_flex my_flex_center cursor"><img @click="addForumFavorites(index)" :src="item.isFavorite?\'imgs/news/love.png\':\'imgs/community/ulove.png\'" alt="" />收藏</li>-->\
									<li class="my_flex my_flex_center cursor"><img @click="like(index,1)"  :src="item.LikeOrUnlike ? \'imgs/community/liked.png\' : \'imgs/community/like.png\' "   alt="" />{{item.Like}}</li>\
									<li class="my_flex my_flex_center cursor"><img @click="like(index,0)" :src="item.LikeOrUnlike==false ? \'imgs/community/unliked.png\' : \'imgs/community/unlike.png\' " alt="" />{{item.Unlike}}</li>\
									<li class="my_flex my_flex_center cursor"><img @click="go_detail(item)" src="imgs/community/back.png" alt="" />{{item.ReplyCount}}</li>\
								</div>\
							</div>\
						</div>\
						<change-page :current_page="page.pageIndex" :page_size="page.pageSize" :page_total="page.pageTotal" @page-change="getPageForumsForApp"></change-page>\
					</div>',
	data: function() {
		return {
			url: {
				getPageForumsForApp: '/api/Forum/GetPageForumsForApp', //帖子分页
				like: '/api/Forum/Like', //赞和采
				addForum: '/api/Forum/AddForum', //帖子回复
				addForumFavorites: '/api/MyFavorites/AddForumFavorites', //收藏帖子
			},
			forum: [], //论坛列表
			page: {
				pageIndex: 1,
				pageSize: 2,
				pageTotal: 0
			}
		}
	},
	mounted: function() {
		this.getPageForumsForApp(this.page.pageIndex, this.page.pageSize);
	},
	methods: {
		//获取帖子
		getPageForumsForApp: function(curPage, pageSize) {
			var that = this;
			var data = {
				PageIndex: curPage,
				PageSize: pageSize
			};
			globalData.ajaxPost(this.url.getPageForumsForApp, data, function(res) {
				var d = res.data;
				if(d == null)
					return;
				globalData.utils.page(that, curPage, res.count);
				for(var i = 0; i < d.length; i++) {
					d[i].HeadImage=globalData.utils.imagePrefix(d[i].HeadImage)
				}
				that.forum = d;
			})
		},
		//收藏帖子
		addForumFavorites: function(index) {
			console.log(this.forum[index])
			var d = this.forum[index];
			if(d.isFavorite)
				return;
			var that = this;
			if(!globalData.userIsLogin(null,true)){
				return;
			}
			var user = globalData.getUserInfo();
			var data = {
				'UserID': user.ID,
				'ForumID': d.ID
			};
			globalData.ajaxPost(this.url.addForumFavorites, data, function(res) {
				if(res.suc) {
					d.isFavorite = true;
					globalData.utils.alert("收藏成功");
				}
			});
		},
		//赞和踩
		like: function(index, num) {
			var d = this.forum[index];
			console.log(d)
			var that = this;
			var b = globalData.userIsLogin(this);
			if(!b)
				return;
			if(d.Like || d.Unlike)
				return;
			var data = {
				ID: d.ID,
				IsLike: num
			};
			globalData.ajaxPost(this.url.like, data, function(res) {
				if(!res.suc)
					return;
				that.getPageForumsForApp(that.page.pageIndex, that.page.pageSize);
				
			});
		},
		//去详情页
		go_detail: function(item) {
			var obj = {
				id:item.ID
			}
			var url = "forum_datail.html"
			globalData.utils.setUrl(url,obj)
		}
	}
});